package com.rfchina.okr.xianren.controller.api;

import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.redis.Redis;
import com.rfchina.okr.xianren.base.BaseNoLoginController;
import com.rfchina.okr.xianren.model.UserInfo;
import com.rfchina.okr.xianren.model.UserMoney;

public class UserInfoController extends BaseNoLoginController {

	// 用户注册
	@Clear(ApiLoginInterceptor.class)
	public void add_user_info() {

		String phone = getPara("phone", "");
		String passwd = getPara("passwd", "");
		String bankName = getPara("bank_name", "");
		String bankNo = getPara("bank_no", "");
		String bankUsername = getPara("bank_username", "");
		String nickname = getPara("nickname", "");
		String appid = getPara("appid", "");

		verification(StrKit.isBlank(phone), "手机号码不能为空");
		verification(StrKit.isBlank(passwd), "密码不能为空");
		verification(!phone.matches("1[0-9]{10}"), "手机号码格式不对!");

		UserInfo info = new UserInfo();

		info.setCreateTime(new Date());
		// info.setMoney(BigDecimal.valueOf(0));
		info.setBankName(bankName);
		info.setBankNo(bankNo);
		info.setBankUsername(bankUsername);
		info.setPasswd(passwd);
		info.setNickname(nickname);
		info.setPhone(phone);
		info.setAppid(appid);
		info.setStatus(1);

		verification(Db.queryLong("select count(*) from user_info where phone=?", phone) > 0, "该手机号已经注册:" + phone);
		info.save();

		Long uid = info.getId();
		UserMoney um = new UserMoney();
		um.setUid(uid);
		um.setLastFlowId(0);
		um.setLocking(0);
		um.setMoney(BigDecimal.valueOf(0));
		um.save();
		myRenderJson(getAllUserInfo(uid));

	}

	// 用户登陆
	@Clear(ApiLoginInterceptor.class)
	public void user_login() {

		String phone = getPara("phone", "");
		String passwd = getPara("passwd", "");

		UserInfo user = UserInfo.dao.findFirst("select * from user_info where phone=? limit 1", phone);

		String msg = "用户或密码错误!";
		int code = 1501;
		verification(user == null, msg, code);
		verification(!passwd.equals(user.getPasswd()), msg, code);
		verification(user.getStatus() != 1, "用户禁用!", 1502);

		// JSONObject json = new JSONObject();
		// json.put("user", user);
		//
		// Integer uid = user.getId();
		// myRenderJson(getAllUserInfo(uid));
		Long uid = user.getId();
		int seconds = 60 * 60 * 24 * 1;
		String key = "xr_l_" + HashKit.generateSalt(60);
		Redis.use().setex(key, seconds, uid);

		JSONObject json = new JSONObject();
		json.put("access_token", key);
		myRenderJson(json);
	}

	private JSONObject getAllUserInfo(long uid) {
		UserInfo bean = UserInfo.dao.findById(uid);
		JSONObject json = new JSONObject();
		bean.remove("passwd");
		json.put("user", bean);
		UserMoney money = UserMoney.dao.findById(uid);
		json.put("money", money);

		return json;
	}

	// 用户资料修改
	public void update_user_info() {
		Long uid = loginUid();
		UserInfo info = UserInfo.dao.findById(uid);
		verification(info == null, "用户信息不存在" + uid);
		String bankName = getPara("bank_name");
		String bankNo = getPara("bank_no");
		String bankUsername = getPara("bank_username");
		String nickname = getPara("nickname");
		if (StrKit.notNull(bankName)) {
			info.setBankName(bankName);
		}
		if (StrKit.notNull(bankNo)) {
			info.setBankNo(bankNo);
		}
		if (StrKit.notNull(bankUsername)) {
			info.setBankUsername(bankUsername);
		}
		if (StrKit.notNull(nickname)) {
			info.setNickname(nickname);
		}
		info.update();
		myRenderJson(getAllUserInfo(uid));
	}

	// 用户资料信息
	public void get_user_info() {
		Long uid = loginUid();
		myRenderJson(getAllUserInfo(uid));
	}

}
